package com.ifacebox.software.updater;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * 软件更新参数设置界面
 *
 * @author znn
 */
public class SoftwareUpdaterSetupDialog extends JDialog implements ActionListener {
    private static final long serialVersionUID = 1L;
    private JTextField softwareUpdaterAddress, softwareUpdaterName;
    private JButton softwareUpdaterSave, softwareUpdaterCancel;
    private SoftwareUpdaterSetupCallback softwareUpdaterSetupCallback;

    public SoftwareUpdaterSetupDialog(SoftwareUpdaterFrame softwareUpdaterFrame) {
        super(softwareUpdaterFrame, softwareUpdaterFrame.getTitle(), true);
        super.setIconImage(softwareUpdaterFrame.getIconImage());
        super.setResizable(false);
        super.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        super.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                softwareUpdaterCancel.doClick();
            }
        });
        super.add(new JPanel() {
            private static final long serialVersionUID = 1L;

            {
                this.add(new Box(BoxLayout.Y_AXIS) {
                    private static final long serialVersionUID = 1L;

                    {
                        this.add(new Box(BoxLayout.X_AXIS) {
                            private static final long serialVersionUID = 1L;

                            {
                                this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
                                this.add(new JLabel("软件升级地址："));
                                this.add(softwareUpdaterAddress = new JTextField(32));
                            }
                        });
                        this.add(new Box(BoxLayout.X_AXIS) {
                            private static final long serialVersionUID = 1L;

                            {
                                this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
                                this.add(new JLabel("软件升级名称："));
                                this.add(softwareUpdaterName = new JTextField(32));
                            }
                        });
                    }
                });
            }
        }, BorderLayout.CENTER);
        super.add(new JPanel() {
            private static final long serialVersionUID = 1L;

            {
                this.setLayout(new FlowLayout(FlowLayout.RIGHT));
                this.add(softwareUpdaterSave = new JButton("保存(S)"));
                this.add(softwareUpdaterCancel = new JButton("取消(N)"));
            }
        }, BorderLayout.SOUTH);
        this.softwareUpdaterName.setEditable(false);
        this.softwareUpdaterSave.setMnemonic('S');
        this.softwareUpdaterSave.addActionListener(this);
        this.softwareUpdaterCancel.setMnemonic('N');
        this.softwareUpdaterCancel.addActionListener(this);
        super.pack();
        super.setLocationRelativeTo(softwareUpdaterFrame);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.softwareUpdaterSave) {
            if (this.softwareUpdaterSetupCallback != null && this.softwareUpdaterSetupCallback.onSoftwareUpdaterSave(this.softwareUpdaterAddress.getText(), this.softwareUpdaterName.getText())) {
                JOptionPane.showMessageDialog(this, "保存设置成功，重启后生效。", this.getTitle(), JOptionPane.INFORMATION_MESSAGE);
                super.setVisible(false);
            }
        }
        if (e.getSource() == this.softwareUpdaterCancel) {
            super.setVisible(false);
        }
    }

    public void show(String softwareUpdaterAddress, String softwareUpdaterName, SoftwareUpdaterSetupCallback softwareUpdaterSetupCallback) {
        this.softwareUpdaterAddress.setText(softwareUpdaterAddress);
        this.softwareUpdaterName.setText(softwareUpdaterName);
        this.softwareUpdaterSetupCallback = softwareUpdaterSetupCallback;
        this.softwareUpdaterCancel.requestFocus();
        super.setVisible(true);
    }

}
